package org.aaron.demos;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.aaron.demos.utils.DownloadHelper;
import org.aaron.demos.xml.MyContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class XmlParseActivity extends Activity {
	private Button parse_xml_btn;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.xmlparselayout);
		initActivity();
	}
	private void initActivity(){
		parse_xml_btn= (Button) this.findViewById(R.id.parse_xml_btn);
		parse_xml_btn.setOnClickListener(new XmlParseClickListener());
	}
	class XmlParseClickListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			DownloadHelper downloadHelper=new DownloadHelper();
			String fileStr=downloadHelper.downloadFile2String("http://192.168.16.222:8080/Example_Web/voa/strings.xml");
			SAXParserFactory saxParserFactory=SAXParserFactory.newInstance();
			try {
				XMLReader reader=saxParserFactory.newSAXParser().getXMLReader();
				reader.setContentHandler(new MyContentHandler());
				reader.parse(new InputSource(new StringReader(fileStr)));			
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
